package com.example.demo.mapper;


import com.example.demo.entity.NoteComment;

import com.example.demo.entity.NoteCreate;
import com.example.demo.vo.ViewNoteCommentInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoteCommentMapper {
     @Select("select note_comment_tb.*,u.user_name,nc.title from note_comment_tb join user_tb u on note_comment_tb.user_id = u.user_id LEFT JOIN note_create_tb nc on note_comment_tb.note_create_id = nc.note_create_id order by note_comment_tb.create_time DESC")
     List<ViewNoteCommentInfo> findNoteCommentInfo();

     @Select("select note_comment_tb.*,u.user_name,nc.title from note_comment_tb join user_tb u on note_comment_tb.user_id = u.user_id LEFT JOIN note_create_tb nc on note_comment_tb.note_create_id = nc.note_create_id  where note_comment_id=#{note_comment_id} order by note_comment_tb.note_comment_id")
     ViewNoteCommentInfo findNoteCommentInfoId(Integer note_comment_id);

    //查找所有评论信息
    @Select("select * from note_comment_tb")
    List<NoteComment> findNoteCommentAll();

    //根据评论id查找评论信息
    @Select("select * from note_comment_tb where note_comment_id=#{note_comment_id}")
    NoteComment findNoteCommentDyId(Integer note_comment_id);

    //与用户表相关联，且根据笔记id查找,按点赞量排序
    @Select("select note_comment_tb.*,u.user_name,u.profile_picture,nc.title from note_comment_tb join user_tb u on note_comment_tb.user_id = u.user_id LEFT JOIN note_create_tb nc on note_comment_tb.note_create_id = nc.note_create_id where note_comment_tb.note_create_id=#{note_create_id} order by note_comment_tb.like_count DESC")
    List<ViewNoteCommentInfo> findNoteCommentInfonoteid(@Param("note_create_id") Integer note_create_id);

    //添加评论页信息
    @Insert("insert into note_comment_tb (note_create_id,user_id,note_comment_content,like_count,comment_count,status) values(#{note_create_id},#{user_id},#{note_comment_content},#{like_count},#{comment_count},#{status})")
    Integer insertNoteComment(NoteComment noteComment);

    //修改评论页信息
    @Update("update note_comment_tb set like_count=#{like_count},note_comment_count=#{note_comment_count} where note_comment_id=#{note_comment_id}")
    Integer updateNoteComment(NoteComment noteComment);

    @Delete("delete from note_comment_tb where note_comment_id=#{note_comment_id}")
    Integer deleteNoteComment(Integer note_comment_id);





    //点击举报按钮将会把评论状态从正常修改为被举报
    @Update("update note_comment_tb set status = '被举报' where note_comment_id = #{note_comment_id}")
    Integer updateStatus(Integer note_comment_id);

    //点赞量更新（增加）
    @Update("UPDATE note_comment_tb SET like_count = like_count + 1 WHERE note_comment_id = #{note_comment_id}")
    Integer incrementLikeCount(@Param("note_comment_id") Integer note_comment_id);

    //取消点赞
    @Update("UPDATE note_comment_tb SET like_count = like_count - 1 WHERE note_comment_id = #{note_comment_id}")
    Integer decrementLikeCount(@Param("note_comment_id") Integer note_comment_id);

    //根据笔记ID计数
    @Select("SELECT COUNT(*) FROM note_comment_tb WHERE note_create_id = #{note_create_id}")
    Integer countcomment(@Param("note_create_id") Integer note_create_id);
}

